package com.fowo.api.syncLingxingApi.service.syncImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fowo.api.entity.ShopInfo;
import com.fowo.api.syncLingxingApi.entity.TmpSyncLingxingToSellerLists;
import com.fowo.api.syncLingxingApi.service.SyncLxApiBaseService;

import com.fowo.api.syncLingxingApi.service.impl.TmpSyncLingxingToSellerListsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@DS("ds24")
public class SyncLxSellerListServiceimpl
        extends SyncLxApiBaseService<TmpSyncLingxingToSellerLists, ShopInfo> {
    private final static int MAX_LEN = 1000;
    private final static String LX_API_URL_KEY = "sellerListsUrl";

    @Resource
    private TmpSyncLingxingToSellerListsServiceImpl tmpService;

    public SyncLxSellerListServiceimpl() {
        super(LX_API_URL_KEY, MAX_LEN);
    }

    @Override
    protected JSONObject pullLingxingData(long offset, long length, JSONObject params) {
        Map<String, Object> reqBody = new HashMap<>();
        return tryRequestLingxingApi(reqBody);
    }

    @Override
    protected void beforeLoop() {
        tmpService.clearAll();
    }

    @Override
    protected boolean cmpNewAndOldEqualCond(TmpSyncLingxingToSellerLists newOne, ShopInfo oldOne) {
        if (newOne == null || oldOne == null) return false;
        
//        String newCode = newOne.getSid() == null? "": newOne.getSid();
//        String oldCode = oldOne.getShopCode() == null? "": oldOne.getShopCode();
//
//        return newCode.equals(oldCode);
        return true;
        
    }

    @Override
    protected TmpSyncLingxingToSellerLists cmpNewAndOldInsertProc(TmpSyncLingxingToSellerLists newOne) {
        newOne.set_flag(2);
        newOne.set_version(this.data_version);
        return newOne;
    }

    @Override
    protected TmpSyncLingxingToSellerLists cmpNewAndOldUpdateProc(TmpSyncLingxingToSellerLists newOne, ShopInfo oldOne) {
        newOne.set_flag(1);
        newOne.set_version(this.data_version);
        return newOne;
    }

    @Override
    protected boolean cmpNewAndOldUpdateCond(TmpSyncLingxingToSellerLists newOne, ShopInfo oldOne) {
        return true;
    }

    @Override
    protected void saveTempToFormal() {
        log.info(">> update data size is " + tmpService.updateToTable(this.data_version));
        log.info("<< insert data size is " + tmpService.insertToTable(this.data_version));

    }

    @Override
    protected List<TmpSyncLingxingToSellerLists> jsonDumpsResult(JSONArray jsonArray) {
        List<TmpSyncLingxingToSellerLists> results = new ArrayList<>();
        for (Object obj: jsonArray) {
            JSONObject item = (JSONObject) obj;
            TmpSyncLingxingToSellerLists tmp = new TmpSyncLingxingToSellerLists();
            tmp.setSid(item.getInteger("sid").toString());
            tmp.setMid(item.getInteger("mid"));
            tmp.setName(item.getString("name"));
            tmp.setSellerId(item.getString("seller_account_id"));
            tmp.setAccountName(item.getString("account_name"));
            tmp.setSellerAccountId(item.getInteger("seller_account_id"));
            tmp.setRegion(item.getString("region"));
            tmp.setCountry(item.getString("country"));
            tmp.setHasAdsSetting(item.getInteger("has_ads_setting"));
            tmp.setMarketplaceId(item.getString("marketplace_id"));
            tmp.setStatus(item.getInteger("status"));
            results.add(tmp);
        }
        return results;
    }

    @Override
    protected List<ShopInfo> getOldData(List<TmpSyncLingxingToSellerLists> newObjects) {
        return tmpService.getOldData(newObjects);
    }

    @Override
    protected void saveData2TmpTb(List<TmpSyncLingxingToSellerLists> results) {
        tmpService.saveBatch(results);
    }
}
